﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace TestEngine
{
    class Utility
    {
        Texture2D pixel;
        Color[] col;

        public Utility(GraphicsDeviceManager graphics)
        {
            pixel = new Texture2D(graphics.GraphicsDevice, 1, 1);
            col = new Color[1];
            col[0] = Color.White;
            pixel.SetData<Color>(col);
        }

        void draw(Vector2 v1, Vector2 v2)
        {
            double deltaX = v2.X - v1.X;
            double deltaY = v2.Y - v1.Y;
            double lineSize = Math.Sqrt(Math.Pow(deltaX, 2) + Math.Pow(deltaY, 2));
            double angle = Math.Atan2(deltaY, deltaX);
        }

        public void drawLine(SpriteBatch sb, Vector2 start, Vector2 end)
        {
            float rot = (float)Math.Atan2(end.Y - start.Y, end.X - start.X);
            Vector2 pos = start;
            float xScale = (float)Math.Sqrt(Math.Pow((end.X - start.X), 2) 
                            + Math.Pow((end.Y - start.Y), 2));

            sb.Draw(pixel, pos, null, col[0], rot, Vector2.Zero, new Vector2(xScale, 2), SpriteEffects.None, 0);
        }

        public void drawRect(SpriteBatch sb, Vector2 start, Vector2 end)
        {
            drawLine(sb, new Vector2(start.X, start.Y), new Vector2(start.X + end.X, start.Y));
            drawLine(sb, new Vector2(start.X + end.X, start.Y), new Vector2(start.X + end.X, start.Y + end.Y));
            drawLine(sb, new Vector2(start.X + end.X, start.Y + end.Y), new Vector2(start.X, start.Y + end.Y));
            drawLine(sb, new Vector2(start.X, start.Y + end.Y), new Vector2(start.X, start.Y));

            /*drawLine(sb, new Vector2(start.X, start.Y), new Vector2(end.X, start.Y));
            drawLine(sb, new Vector2(end.X, start.Y), new Vector2(end.X, end.Y));
            drawLine(sb, new Vector2(end.X, end.Y), new Vector2(start.X, end.Y));
            drawLine(sb, new Vector2(start.X, end.Y), new Vector2(start.X, start.Y));*/
        }

        public void drawAABB(SpriteBatch sb, Vector2 start, Vector2 end)
        {
            float minX;
            float maxX;
            float minY;
            float maxY;

            if (start.X < end.X)
            {
                minX = start.X;
                maxX = end.X;
            }
            else
            {
                minX = start.X;
                maxX = end.X;
            }
            if (start.Y < end.Y)
            {
                minY = start.Y;
                maxY = end.Y;
            }
            else
            {
                minY = start.Y;
                maxY = end.Y;
            }

            drawLine(sb, new Vector2(minX, minY), new Vector2(maxX, minY));
            drawLine(sb, new Vector2(maxX, minY), new Vector2(maxX, maxY));
            drawLine(sb, new Vector2(maxX, maxY), new Vector2(minX, maxY));
            drawLine(sb, new Vector2(minX, maxY), new Vector2(minX, minY));
        }
    }
}
